const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      Enterprise_Info_SerialNumber: $.cookie("enterprise"),
      integral_unitprice: {
        Integral_Unit_Price_SerialNumber: "",
        Start_Date: "",
        End_Date: "",
        Children: {
          integral_unitprice_detail: [
            {
              Integral_Unit_Price_Detail_SerialNumber: "",
              Integral_Unit_Price_SerialNumber: "",
              Article_SerialNumber: "",
              Article_Specifications_SerialNumber: "",
              Unit_Price: "",
              Integral_Unit_Price: "",
              Sign: "0",
              Distribute: 0,
              Children: {
                integral_unitprice_stock: [
                ]
              }
            }
          ],
          integral_scheme_storescope: [
            {
              Integral_Unit_Price_StoreScope_SerialNumber: "",
              Integral_Unit_Price_SerialNumber: "",
              Store_SerialNumber: "",
            }
          ],
        }
      },
      rules: [],
      shop: [],
      res: [{
        "Article_SerialNumber": "A00000000000006",
        "Article_Name": "玉木耳",
        "Picture_URL": "\/dd3ed91420231223111501.jpeg",
        "Row_Status": "0",
        "Category_Name": "乳制品",
        "Brand_Name": "白象",
        "Item_Name": "正常款",
        "Series_Name": "日用品系列",
        "Unit_Price": "0.1",
        "Enterprise_Info_SerialNumber": "E00000000000017",
        "Enterprise_Name": "黑龙江集货邦数字供应链有限公司",
        "Release_Type": "1",
        "Children": {
          "article_specifications": []
        }
      },
      {
        "Article_SerialNumber": "A00000000000007",
        "Article_Name": "西瓜",
        "Picture_URL": "",
        "Row_Status": "0",
        "Category_Name": "水果",
        "Brand_Name": "白象",
        "Item_Name": "正常款",
        "Series_Name": "生鲜系列",
        "Unit_Price": "1231",
        "Enterprise_Info_SerialNumber": "E00000000000015",
        "Enterprise_Name": "赵",
        "Release_Type": "0",
        "Children": {
          "article_specifications": []
        }
      },
      {
        "Article_SerialNumber": "A00000000000008",
        "Article_Name": "嗲哇",
        "Picture_URL": "",
        "Row_Status": "0",
        "Category_Name": "水果",
        "Brand_Name": "白象",
        "Item_Name": "正常款",
        "Series_Name": "酒类系列",
        "Unit_Price": "123",
        "Enterprise_Info_SerialNumber": "E00000000000015",
        "Enterprise_Name": "赵",
        "Release_Type": "0",
        "Children": {
          "article_specifications": []
        }
      },
      {
        "Article_SerialNumber": "A00000000000009",
        "Article_Name": "双汇熏烤肉肠蒜香味300g",
        "Picture_URL": "\/C6902890267591_1.png",
        "Row_Status": "0",
        "Category_Name": "大米",
        "Brand_Name": "白象",
        "Item_Name": "",
        "Series_Name": "",
        "Unit_Price": "9",
        "Enterprise_Info_SerialNumber": "E00000000000016",
        "Enterprise_Name": "海伦市世纪华联超市",
        "Release_Type": "0",
        "Children": {
          "article_specifications": []
        }
      },
      {
        "Article_SerialNumber": "A00000000000010",
        "Article_Name": "梅林午餐肉340g",
        "Picture_URL": "\/C6902131110020_1.png",
        "Row_Status": "0",
        "Category_Name": "大米",
        "Brand_Name": "白象",
        "Item_Name": "",
        "Series_Name": "",
        "Unit_Price": "10.1667",
        "Enterprise_Info_SerialNumber": "E00000000000016",
        "Enterprise_Name": "海伦市世纪华联超市",
        "Release_Type": "0",
        "Children": {
          "article_specifications": []
        }
      }
      ],
      isDisabled: true,
      specs: [],//商品规格
      Distribute: 0,//分配数量
      store: [],//适用店铺
      always: "",//补货时间
      rules: {
        Start_Date: [
          { required: true, message: '请输入开始时间', trigger: 'blur' },
        ],
        End_Date: [
          { required: true, message: '请输入结束时间', trigger: 'change' }
        ],
        always: [
          { required: true, message: '请输入补货时间', trigger: 'change' }
        ],
        // pp: [
        //   { required: true, message: '请选择商品分类方式', trigger: 'blur' }
        // ],
        // zhongli: [
        //   { required: true, message: '请选择适用商品类', trigger: 'blur' }
        // ],
      },
      Article_SerialNumber: [
        { required: true, message: '请选择商品', trigger: 'change' }
      ],
      Article_Specifications_SerialNumber: [
        { required: true, message: '请选择规格', trigger: 'change' }
      ],
      Integral_Unit_Price: [
        { required: true, message: '请输入所需积分', trigger: 'change' }
      ],
      Distribute: [
        { required: true, message: '请输入分配数量', trigger: 'change' }
      ],
      Store_SerialNumber: [
        { required: true, message: '请输入适用店铺', trigger: 'change' }
      ]
    }
  },
  mounted() {

  },
  computed: {
    formatTime() {
      return function (time) {
        let d = new Date(time);
        let year = d.getFullYear();
        let month = d.getMonth() + 1
        let day = d.getDate()
        let hour = d.getHours()
        let minute = d.getMinutes()
        let second = d.getSeconds()
        if (month < 10) { month = '0' + month }
        if (day < 10) { day = '0' + day }
        if (hour < 10) { hour = '0' + hour }
        if (minute < 10) { minute = '0' + minute }
        if (second < 10) { second = '0' + second }
        return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
      };
    },
  },
  methods: {
    //适用店铺
    add_integral_scheme_storescope() {
      this.integral_unitprice.Children.integral_scheme_storescope.push({
        Integral_Unit_Price_StoreScope_SerialNumber: "",
        Integral_Unit_Price_SerialNumber: "",
        Store_SerialNumber: "",
      })
    },
    remove_integral_scheme_storescope() {
      if (this.integral_unitprice.Children.integral_scheme_storescope.length > 1) {
        this.integral_unitprice.Children.integral_scheme_storescope.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    // 新增积分销售价格单明细
    add_integral_unitprice_detail() {
      this.integral_unitprice.Children.integral_unitprice_detail.push({
        Integral_Unit_Price_Detail_SerialNumber: "",
        Integral_Unit_Price_SerialNumber: "",
        Article_SerialNumber: "",
        Article_Specifications_SerialNumber: "",
        Unit_Price: "",
        Integral_Unit_Price: "",
        Sign: "",
        Distribute: 0,
        Children: {
          integral_unitprice_stock: [
          ]
        }
      })
    },
    remove_integral_unitprice_detail() {
      if (this.integral_unitprice.Children.integral_unitprice_detail.length > 1) {
        this.integral_unitprice.Children.integral_unitprice_detail.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    //请求商品
    getData() {
      let _this = this;
      this.res = this.res.filter(function (item) {
        return item.Row_Status == '0'
      })
      this.res.forEach(item => {
        item.Article_Name = item.Article_Name + '(所属企业:' + item.Enterprise_Name + ')'
      })
      for (let i = 0; i < this.res.length; i++) {
        _this.shop.push(this.res[i]);
      }
      // $.ajax({
      //   url: "../../../../../jf.json",
      //   type: 'POST',
      //   dataType: 'json',
      //   data: { token: $.cookie("token") },
      //   success: function (res) {
      //     console.log(res)
      //     res = res.filter(function (item) {
      //       return item.Row_Status == '0'
      //     })
      //     res.forEach(item => {
      //       item.Article_Name = item.Article_Name + '(所属企业:' + item.Enterprise_Name + ')'
      //     })
      //     for (let i = 0; i < res.length; i++) {
      //       _this.shop.push(res[i]);
      //     }
      //     console.log(_this.shop);
      //   },
      //   complete: function (XMLHttpRequest) {
      //     _this.loading = false
      //     if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
      //       window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
      //     }
      //   }
      // })
      $.ajax({
        type: 'POST',
        url: DomainName + "/getInquiryAction",
        data: {
          Data: "store_info",
          token: $.cookie("token")
        },
        success: function (res) {
          res = res.filter(function (item) {
            return item.Row_Status != '1'
          })
          for (let i = 0; i < res.length; i++) {
            _this.store.push(res[i]);
          }
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    //商品规格数据
    Change_Unit_Price(index) {
      // this.isDisabled = true
      console.log(this.shop[index].Article_SerialNumber);
      let str = this.integral_unitprice.Children.integral_unitprice_detail[index].Article_SerialNumber
      this.integral_unitprice.Children.integral_unitprice_detail[index].Article_Specifications_SerialNumber = ''
      let _this = this;
      $.ajax({
        type: 'POST',
        url: DomainName + "/SpecificationsIDAction",
        data: {
          Article_SerialNumber: str,
          token: $.cookie("token")
        },
        success: function (res) {
          _this.specs = res
          console.log(res)
          _this.isDisabled = false
        },
        complete: function (XMLHttpRequest) {
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    Change_Specifications_Name(item, event) {//吊牌价
      console.log(item, event);
      item.Unit_Price = this.specs.find(item => item.Article_Specifications_SerialNumber === event).Unit_Price;
    },
    Automatic(item, Start_Date, End_Date, time) {
      console.log(item, Start_Date, End_Date, time);
      //一天毫秒数
      let TimeDays = (1000 * 60) * 60 * 24
      if (+new Date(End_Date) <= +new Date(time)) {//若结算时间小于次日补货时间
        item.Children.integral_unitprice_stock.push({
          Integral_Unit_Price_Stock_SerialNumber: "",
          Integral_Unit_Price_Detail_SerialNumber: "",
          Start_Date: Start_Date,
          End_Date: time,
          Distribute: item.Distribute,
          Stock_Remain: item.Distribute,
          Stock_Sell: 0
        })
      } else {//若结算时间大于次日补货时间
        item.Children.integral_unitprice_stock.push({
          Integral_Unit_Price_Stock_SerialNumber: "",
          Integral_Unit_Price_Detail_SerialNumber: "",
          Start_Date: Start_Date,
          End_Date: time,
          Distribute: item.Distribute,
          Stock_Remain: item.Distribute,
          Stock_Sell: 0,
        })
        this.Automatic(item, time, End_Date, this.formatTime(+new Date(time) + TimeDays))
      }
    },
    submitForm(formName) {
      console.log(1);
      let _this = this;
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 表单验证通过，执行提交操作
          let TimeDays = (1000 * 60) * 60 * 24;
          let time = `${_this.integral_unitprice.Start_Date.substring(0, 10)} ${_this.always}`;

          if (+ new Date(time) < +new Date(_this.integral_unitprice.Start_Date)) {
            time = _this.formatTime(+new Date(time) + TimeDays)
          }

          _this.integral_unitprice.Children.integral_unitprice_detail.forEach(function (item, index) {
            console.log(item)
            if (item.Sign == "0") {
              _this.Automatic(item, _this.integral_unitprice.Start_Date, _this.integral_unitprice.End_Date, time)
            }
          })
        } else {
          // 表单验证失败，显示错误信息
          console.log('表单验证失败，请检查表单填写');
        }
      });
      console.log(_this.integral_unitprice)
    },
  },
  created() {
    this.getData();
  }
})